﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SES.CMS.DO;
using SES.CMS.BL;
using Telerik.Web.UI;
using Telerik.Web.UI.Editor.DialogControls;
using System.Data;


namespace SES.CMS.AdminCP
{
    public partial class admincp : System.Web.UI.MasterPage
    {
        MenuAdminsDO objMenuAdmin = new MenuAdminsDO();
        PhanQuyenDO objPhanquyen = new PhanQuyenDO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] != null)
            {
                int UserID = int.Parse(Session["UserID"].ToString());
                string url = Request.Url.ToString();
                url = url.Substring(1, url.Length - 1);
             
                string Module = url.Substring(url.LastIndexOf("/")+1);
                if (Module.ToLower().Equals("default.aspx"))
                {
                    rptMenuChaDatasouect();
                    rptmenulefdatasouect();
                }
                else if (int.Parse(Session["UserType"].ToString()) == 1)
                {
                    rptMenuChaDatasouect();
                    rptmenulefdatasouect();
                }
                else
                {
                    DataTable dtUrl = new PhanQuyenBL().CheckPhanQuyen(UserID, Module);
                    if (dtUrl.Rows.Count > 0)
                    {

                        rptMenuChaDatasouect();
                        rptmenulefdatasouect();
                    }
                    else
                    {
                        Response.Redirect("Default.aspx");
                    }
                }
            }
            else
            {
                Response.Redirect("/AdminCP/Login.aspx");
            }
            //else
            //{
            //    lblUserName.Text = Session["UserName"].ToString();
            //    int userType = int.Parse(Session["UserType"].ToString());
            //    if (userType <= 3)
            //    {
            //        LoadMenu(userType);
            //    }
            //    else
            //    {
            //        Response.Redirect("/Default.aspx");
            //    }
            //}
            //Session["UserName"] = "Admin";
            //Session["UserID"] = "1";

        }

        private void LoadMenu(int userType)
        {
            throw new NotImplementedException();
        }
        protected void lbtnLogout_Click(object sender, EventArgs e)
        {
            Session["UserName"] = null;
            Session["UserID"] = null;
            Session["UserType"] = null;
            Session.Abandon();
            Response.Redirect("/AdminCP/Login.aspx");
        }
        protected void rptMenuChaDatasouect()
        {
            if (Session["UserType"].ToString() != "1")
            {
                int UserId = int.Parse(Session["UserID"].ToString());
                DataTable dtPhanquyen = new PhanQuyenBL().Select(UserId);

                rptMenuCha.DataSource = new PhanQuyenBL().SelectMenu(UserId);
                rptMenuCha.DataBind();

            }
            else
            {

                rptMenuCha.DataSource = new MenuAdminsBL().SelectGetByTop("", "ParentID=0", "");
                rptMenuCha.DataBind();
            }
        }
        protected string FriendlyUrl(string s)
        {
            return Ultility.Change_AV(s);
        }

        protected void rptMenuCha_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {

            DataRowView dv = e.Item.DataItem as DataRowView;

            if (dv != null)
            {
                string cName = dv["MenuAdminID"].ToString();
                Repeater rptMenuCon = (Repeater)e.Item.FindControl("rptMenuCon");
                //if (Session["UserType"].ToString() == "0")
                //{
                //    rptMenuCon.DataSource = new MenuAdminsBL().SelectGetByTop("", "ParentID=" + cName, "");
                //    rptMenuCon.DataBind();
                //}
                //else
                //{
                if (rptMenuCon != null)
                {
                    int UserMenuId = int.Parse(Session["UserID"].ToString());

                    DataTable dtPhanquyen = new MenuAdminsBL().SelectGetByTop("", "ParentID=" + cName, "");
                    rptMenuCon.DataSource = dtPhanquyen;
                    rptMenuCon.DataBind();
                }
                //}
            }



        }
        protected void rptmenulefdatasouect()
        {
            if (Session["UserType"].ToString() != "1")
            {
                int UserMenuId = int.Parse(Session["UserID"].ToString());
                objPhanquyen.MenuID = UserMenuId;
                rptMenuleft.DataSource = new PhanQuyenBL().SelectMenuLef(UserMenuId);
                rptMenuleft.DataBind();
                //for (int i = 0; i < dtPhanquyen.Rows.Count; i++)
                //{
                //    string MenuAdminid = dtPhanquyen.Rows[i]["MenuAdminID"].ToString();
                //    rptMenuleft.DataSource = new MenuAdminsBL().SelectGetByTop("", "ParentID<>0 and " + "UserType=1 and " + "ParentID=" + MenuAdminid, "");
                //    rptMenuleft.DataBind();
                //}

            }
            else
            {

                rptMenuleft.DataSource = new MenuAdminsBL().SelectGetByTop("", "ParentID<>0 and UserType=1", "");
                rptMenuleft.DataBind();
            }
        }
    }
}